Ext.define('jwawa.erp.home.HomeHandler', {
    extend : "jwawa.erp.framework.ShellHandler",
    bindHandler: function() {
        var me = this,
            view = me.view;

        me.callParent(arguments);

        top.westPanel = Ext.getCmp("westPanel");
        top.menuFilterInputPanel = Ext.ComponentQuery.query('#menuFilterInputPanel', view)[0];
        top.menuPanel = Ext.ComponentQuery.query('#menuPanel', view)[0];
        top.contentPanel = Ext.ComponentQuery.query('#contentPanel', view)[0];
        top.taskMsgText = Ext.getCmp("taskMsgText");
        top.msgBar = Ext.getCmp('msgBar');

        me.initMenuTreePanel();

        view.down("#logoffBtn").on("click", me.logoff);
        view.down("#changePwdMi").on("click", me.changePwd);
        view.down("#userProfileMi").on("click", me.editUserProfile);
        view.down("#logoffMi").on("click", me.logoff);

        // 添加消息
//        top.taskMsgText.setTextParam({taskCount: 200, loginName : loginAccount['employeeName']});
        top.taskMsgText.setTextParam({loginName : loginAccount['employeeName']});

        // 添加LogonPage
        me.initLogonPage();

        Ext.defer(function(){top.contentPanel.addBodyCls('center-region-body-img')}, 1000);
    },
    initMenuTreePanel: function(){
        Jwawa.framework.JwawaConnection.request({
            url      : '/home/find-user-menu-tree',
            async    : true,
            method: 'GET',
            success : function(response) {
                var menuRoot = response["data"];
                menuRoot['expanded'] = true;

                var fieldsCfg = [
                    {name: 'id',  type: 'string'},
                    {name: 'text', mapping: 'menu_name', type: 'string'},
                    {name: 'short_name',  type: 'string'},
                    {name: 'parent_id', type: 'string'},
                    {name: 'page_code', type: 'string'},
                    {name: 'iconCls', mapping: 'icon', type: 'string'},
                    {
                        name: 'leaf', mapping: 'is_leaf',
                        convert: function(value, record) {
                            return value == 'Y' ? true : false;
                        }
                    }
                ];
                var menuTreeStore = Ext.create('Ext.data.TreeStore', {
                    fields: fieldsCfg,
                    root: menuRoot
                });

                var menuTree = Ext.create('Ext.tree.Panel', {
                    id: 'system-menu',
                    store: menuTreeStore,
                    xtype: 'treepanel',
                    border: true,
                    animate: false,
                    rootVisible: false,
                    listeners: {
                        itemdblclick: function(view, record, item, index, e, eOpts) {
                            if (record.get("leaf") == true) {
                                find_or_create_tab_content(record.get("text"),
                                    FUNCTION_URL_PREFIX, record.get("page_code"),
                                    record.get("iconCls"));
                            }
                        },
                        itemclick: function(view, record,  item, index, e, eOpts){
                            if (record.get("leaf") == true && e.ctrlKey) {
                                top.msgBar.showInfoMsg('【菜单名称】' + record.get("text") + ' , 【缩写】' + record.get("short_name"));
                            }
                        }
                    }
                });
                top.menuPanel.add(menuTree);

                // 树形菜单过滤
                var menuFilterInput =  Ext.getCmp('menuFilterInput');
                menuFilterInput.on('inputChange', function(obj, e) {
                    if(obj.getValue() != null && obj.getValue() != "") {
                        Ext.getCmp("system-menu").filterBy(obj.value, 'short_name');
                    } else {
                        Ext.getCmp("system-menu").clearFilter();
                    }
                });
            }
        });
    },
    initLogonPage: function(){
        Jwawa.framework.JwawaConnection.request({
            url      : '/home/get-logon-menu',
            async    : true,
            method: 'GET',
            success : function(response) {
                var logonMenu = response["data"];
                if(logonMenu){
                    find_or_create_tab_content(logonMenu["menu_name"],
                        FUNCTION_URL_PREFIX, logonMenu["page_code"],
                        logonMenu["icon"]);
                }
            }
        });
    },

    logoff: function() {
        Ext.Msg.confirm("提示", "真的要退出系统吗？", function(btn) {
            if (btn == 'yes') {
                top.location = '/home/logoff';
            }
        });
    },

    changePwd: function(){
        var win = find_or_create_win("jwawa.erp.home.ChangePasswordShell",{modal:false});
        showWindow(win, false);
    },

    editUserProfile: function(){
        var s = "<a href='http://www.google.com'>aaa</a>";//Ext.String.repeat("a", 200);
        top.msgBar.showSuccessMsg(s);
    }
});